<%@ page language="java" contentType="text/html" pageEncoding="utf-8"%>
<%@include file="/WEB-INF/pages/common/common.jsp"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@include file="/WEB-INF/pages/common/head.jsp"%>
<script type="text/javascript" src="${ctx }/js/utils/json.js"></script>
<script type="text/javascript">
	//表单数据
	var datagrid;
	//本页面的提示信息
	var message = $.messager;
	var customerCombogrid;
	//弹出Dialog
	var entityDialog;
	$(function() {
		//加载表格的数据
		datagrid = $('#datagrid').datagrid({
			toolbar : '#tb',
			remoteSort: true,          //是否服务器排序
			showFooter : true,
			pagination : false,
			pageList : [ DATAGRIDMAXPAGE ],
			rowStyler : function(index, row) {
				if (row.hasError) {
					return "background:#ffc0cb;";
				}
			}
		});
		 $("#file").change(function(){
			 removeAll();
				var excel = $(this).val();
				var right = true;
				if (!excel) {
					right = false;
				}
				var ps = excel.split(".");
				if (ps.length < 2) {
					right = false;
				} else {
					var format = ps[ps.length - 1].toLowerCase();
					if (format != "xls") {
						right = false;
					} else {
						var url ='${path}/excel/partExcel/excelUpload.do';
						$("#supplierForm").attr("action",url);
						$("#supplierForm").submit();
						imonJYRH.showZone();
					}
				}
				if (!right) {
					$.messager.alert("错误", "请选择上传xls格式的文件!", "error");
					return false;
				}	
			});
	});
     function partTypeFormatter(value,row,index){
         <c:forEach var="dict" items="${dictPartTypeList}">
             if(row['partType']=='${dict.code}'){
                 return '${dict.name}';
             }
         </c:forEach>
     }
     function productTypeFormatter(value,row,index){
         <c:forEach var="dict" items="${dictProductTypeList}">
             if(row['productType']=='${dict.code}'){
                 return '${dict.name}';
             }
         </c:forEach>
     }
	//  模版下载
	function excelModel() {
		$("#supplierForm").attr("action","${path}/excel/partExcel/excelModel.do").submit();
	}
	function saveAll(docheck) {
		//收集数据
		var effectData = new Object();
		var msg="";
		var rows = datagrid.datagrid('getRows');
		if (rows.length) {
			for ( var i = 0; i < rows.length; i++) {
				if(rows[i].hasError){				
					$.messager.alert('错误', '数据有错误，请修改正确后重新导入！', 'error');
					return;
				}
			}
			imonJYRH.showZone();
			$.ajax({
						url:'${path}/excel/partExcel/ajaxSave.do',
						data : effectData,
						cache : false,
						type : 'POST',
						async : true,
						dataType : "json",
					    success : function(reMsg) {
					    	imonJYRH.hideZone();
					    	if(reMsg.success){			
					    		parent.message.show({msg :'保存成功',title : '提示',showType:'slide'});
					    		parent.reloadFrame();
								parent.returnResult();
					    	} else {
					    		if (reMsg.obj) {
						    		$.messager.confirm('提示', reMsg.obj, function(r){
						    			if (r) saveAll(false);
					    			});
					    		} else {
					    			parent.message.show({msg : '<font color=red>' + reMsg.msg + '</font>' ,title : '错误',showType:'slide'});
					    		}
					    	} 
		                }
				});			
			
		} else {
			$.messager.alert('提示', '请添加明细');
		}
	}
	function OnUploadCompleted(reMsg){
		imonJYRH.hideZone();
		reMsg = eval('(' + reMsg + ')'); 
		if ((reMsg.obj) && (reMsg.obj.tips)) {
			$.messager.confirm('提示', reMsg.obj.tips, function(r){
				if (r) 
					uploadFile(false);
			});
		}
		if ((reMsg.obj) && (reMsg.obj.details) && (reMsg.obj.details.length > 0)) {
			var obj = {
				'total' : reMsg.obj.details.length,
				'rows' : reMsg.obj.details,
				'footer' : reMsg.obj.footer
			};
			datagrid.datagrid('loadData', obj);
			//calTotal();
		}
		if ((reMsg.obj) && (reMsg.obj.errors) && (reMsg.obj.errors.length > 0)) {
			var str = '<ul>';
			for (var i=0;i<reMsg.obj.errors.length;i++) {
				str += '<li style="color:red;">'+reMsg.obj.errors[i] + "</li>";
			}
			str += '</ul>';
			$("#win").window('open');
			document.getElementById("message").innerHTML=str;
		}
		if(reMsg.success){
			message.show({msg : '上传成功！<br/>' + reMsg.msg ,title : '提示',showType:'slide'});
		} else {		
			if (reMsg.msg) {
				var s = '<font color=red>' + reMsg.msg + '</span>';
				message.show({msg : s ,title : '错误',showType:'slide'});
			}
		}
	}
	function removeAll() {
		$('#datagrid').datagrid('loadData', {
			total : 0,
			rows : []
		});
		$('#datagrid').datagrid('reload');
	}
</script>
</head>
<body class="easyui-layout">
		<form id="supplierForm" enctype="multipart/form-data" target="target" method="post">
			<div data-options="region:'north'" title="" style="overflow:hidden;">
				<div id="searchbar" class="datagrid-toolbar" style="height: 80;">
			    <fieldset>
				<table class="tableForm" id="tableForm" width="100%">
				<legend><spring:message code='msg.import'/></legend>
					<tr>
						<td align="right"><span class="red">*</span><spring:message code='label.upload.filePath'/></td>
						<td>
							<input id="file" type="file" onchange="getFileName();" name="file"/> 
						</td>
					</tr>
					</table>
					</fieldset>
				</div>
			</div>
			<div data-options="region:'center'">
				<div id="tb">
					<a class="easyui-linkbutton" href="javascript:void(0);" plain="true" iconCls="icon-save" onclick="saveAll();"><spring:message code='button.save'/></a>
					<a class="easyui-linkbutton" iconCls="icon-ok" onclick="excelModel()"plain="true" href="javascript:void(0);"><spring:message code='button.templetDownload'/></a> 
				</div>
				<table id="datagrid"  fit="true" nowrap="true" rownumbers="false" pagination="true" singleSelect="true" idField="row">
					<thead>
						<tr>
							<th width="30" align="center" fitColumns="true" field="row">行号</th>
							<th data-options="align:'center',width:'100',field:'code'"><spring:message code="table.head.part.code"/></th>
                            <th data-options="align:'center',width:'100',field:'name'"><spring:message code="table.head.part.name"/></th>
                            <th data-options="align:'center',width:'100',field:'partType',formatter:partTypeFormatter"><spring:message code="table.head.part.part.type"/></th>
				            <th data-options="align:'center',width:'110',field:'productType',formatter:productTypeFormatter"><spring:message code="table.head.part.product.type"/></th>
				            <th data-options="align:'center',width:'110',field:'drawingCode'"><spring:message code="table.head.part.drawing.code"/></th>
				            <th data-options="align:'center',width:'110',field:'drawingVersionCode'"><spring:message code="table.head.part.drawing.version.code"/></th>
				            <th data-options="align:'center',width:'110',field:'supplierCode'"><spring:message code="table.head.supplier.code"/></th>
							<th width="100" align="right" fitColumns="true" field="hasError" hidden="true">error</th>
						</tr>
					</thead>
				</table>
			</div>
		</form>
		<div id="entityDialog" class="easyui-dialog" title="My Dialog" style="width:1000px;height:500px;" data-options="iconCls:'icon-save',resizable:true,modal:true,closed: true">
				<iframe scrolling="auto" id='openEntityCUIframe' frameborder="0" src="" style="width:100%;height:100%;"></iframe>
		</div>
		<div style="z-index: -1; position: absolute; left: 0; top: 0;display: none;">
		<iframe id="target" name="target" frameborder="12">
		</iframe>
	   </div>
	   <div id="win" class="easyui-window" style="width:600px;height:500px;" data-options="iconCls:'icon-save',resizable:true,modal:true,closed: true" title="导入错误提示">  
			<div id="message">
			</div>
		</div>
</body>
</html>